/*******************************************************************************
 * Package: com.hngtrust.dualrecord.user.service.impl
 * Type:    LoginUserServiceImpl
 * Date:    2019/11/06 10:41
 * 
 * Copyright (c) 2019 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 * 
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.service.impl;

import com.hngtrust.framework.security.api.LoginUserService;
import com.hngtrust.framework.security.entity.LoginUser;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.UserService;
import com.hngtrust.iir.util.RsaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Chentianyong
 */
@Service
@Slf4j
public class LoginUserServiceImpl implements LoginUserService {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    //OA验证key
    @Value("${oa.login.private-key}")
    private String privateKey;
    
    @Override
    public LoginUser queryUser(String userName, Map loginFullInfo) {
        //如果系统自己的登录
        if(null != loginFullInfo.get("type") && loginFullInfo.get("type").toString().equals("1")){
            User t = new User();
            t.setUserCode(userName);
            t.setStatus(0);
            List<User> list = userService.queryList(t);
            if(!list.isEmpty()) {
                User user = list.get(0);
                LoginUser loginUser = new LoginUser();
        
                loginUser.setId(user.getId().toString());
                loginUser.setName(userName);
                loginUser.setPassword(user.getPassword());
        
                return loginUser;
            }
        }
        else {
            //解密
            String decryptStr = "";
            String pw = loginFullInfo.get("password").toString();
            try {
                decryptStr = RsaUtils.decryptByPriKey(pw, privateKey);
            } catch (Exception e) {
                log.error("登录解密异常:{}", e);
                return null;
            }
            if (!decryptStr.equals(userName + "IIR")) {
                return null;
            }
    
            User t = new User();
            t.setUserCode(userName);
            //t.setPassword(loginFullInfo.get("password").toString());
            t.setStatus(0);
            List<User> list = userService.queryList(t);
            if (!list.isEmpty()) {
                User user = list.get(0);
                LoginUser loginUser = new LoginUser();
        
                loginUser.setId(user.getId().toString());
                loginUser.setName(userName);
                loginUser.setPassword(passwordEncoder.encode(pw));
    
                return loginUser;
            }
        }
        return null;

    }

}
